package com.example.web;


import com.example.Util.SampleUtil;
import com.example.fanqie.entity.User;
import com.example.fanqie.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
public class UserDao {

    @Resource
    private UserMapper userMapper;

    @Resource
    private SampleUtil sampleUtil;

    @Autowired
    StringRedisTemplate redisTemplate;



    @RequestMapping("/sms")
    public String sendSms(@RequestParam("phone") String phone) throws Exception {

        if(phone == null || phone.isEmpty()){
            return "手机号不能为空";
        }

        String str = sampleUtil.generateRandomCode();

        sampleUtil.sendSms(phone, str);

        return  str;

    }

    @RequestMapping("/login")
    public Integer login(@RequestParam("phone") String phone){
        if(phone == null || phone.isEmpty()){
            return  0;
        }
        User login = userMapper.login(phone);
        if(login != null){

            return login.getUserId();
        }


        User user = new User();
        user.setPhone(phone);
        String password = phone.substring(phone.length() - 6);
        user.setPassword(password );
        user.setUsername("用户"+phone);
        int user1 = userMapper.insert(user);
        if(user1 == 1){
            User login1 = userMapper.login(phone);

            return login1.getUserId();
        }
        return 0;

    }


    @RequestMapping("pwdlogin")

    public Integer pwdlogin(@RequestParam("phone") String phone, @RequestParam("password") String password){
        if(phone == null || phone.isEmpty()){
            return  0;
        }
        User pwdlogin = userMapper.pwdlogin(phone, password);
        if(pwdlogin != null){
            return pwdlogin.getUserId();
        }
        return 0;


    }


    @RequestMapping("iflogin")
    public Object iflogin( HttpServletRequest request){
        if(request.getHeader("userId") == null){
            return null;
        }
        return request.getHeader("userId");

    }

    @RequestMapping("logout")
    public void logout(){
        redisTemplate.delete("jwt");

    }


    @RequestMapping("getUser")
    public User getUser(@RequestParam("userId")  Integer userId){
        User user = userMapper.getUser(userId);
        return user;
    }

    @RequestMapping("updateUser")
    public int updateUser(@RequestBody User user){
        int i = userMapper.updateById(user);
        return i;

    }

    @RequestMapping("adminlogin")
    public Integer adminlogin(@RequestParam("phone") String phone, @RequestParam("password") String password){
        if(phone == null || phone.isEmpty()){
            return  0;
        }
        User adminlogin = userMapper.adminlogin(phone, password);
        if(adminlogin != null){
            return adminlogin.getUserId();
        }
        return 0;




    }




}
